package cfs.model;

import org.mybeans.dao.DAOException;
import org.mybeans.dao.GenericDAO;
import org.mybeans.factory.BeanFactory;
import org.mybeans.factory.RollbackException;
import org.mybeans.factory.Transaction;

import cfs.databean.Employee;

public class EmployerDAO extends GenericDAO<Employee> {

	public EmployerDAO() {
		super(Employee.class, "employee", "username");
		getTable().setIdleConnectionCleanup(true);
	}

	private BeanFactory<Employee> factory;

	public Employee[] getUsers() throws DAOException {
		try {
			Employee[] users = factory.match();
			return users;
		} catch (RollbackException e) {
			throw new DAOException(e);
		}
	}

	public void setPassword(String employee_username, String password)
			throws DAOException {
		try {
			Transaction.begin();
			Employee db_emp = getFactory().lookup(employee_username);
			if (db_emp == null) {
				throw new DAOException("Employee id " + employee_username
						+ "no longer exists");
			}
			db_emp.setPassword(password);
			Transaction.commit();
		} catch (RollbackException e) {
			throw new DAOException(e);
		} finally {
			if (Transaction.isActive())
				Transaction.rollback();
		}
	}
}
